<?php

namespace App\Http\Controllers;

use App\Common\Constant\Params;
use App\Models\OrderRefund;
use App\Models\OrderStatus;
use App\Services\IOrderService;
use App\Services\IRefundService;
use App\Services\IWalletService;
use Illuminate\Http\Request;

/**
 * 退款管理
 * Class RefundController
 * @package App\Http\Controllers
 */
class RefundController extends BaseController
{

    /**
     * 退款管理首页
     * @param Request $request
     */
    public function index(Request $request,IRefundService $refundService){
        $listRefund = $refundService->listRefund(getCurrentUserid());
        return view('refund/index',['listRefund'=>$listRefund]);
    }

    /**
     * 退款详情
     * @param $order_id
     * @param Request $request
     * @param IOrderService $orderService
     */
    public function detail(OrderRefund $refund, Request $request,IOrderService $orderService,IRefundService $refundService)
    {
        if (OrderStatus::where('order_id',$refund->order_id)->first()->closed){//取消或已退款陈功
            return redirect(url("order/except/{$refund->order_id}"));
        }
        if ($request->isMethod('post') && $request->has('refund_result_type')){
            if ($request->input('refund_result_type') == Params::ORDER_REFUND_RESULT_AGREE){
                $refundService->agreeRefund($refund->id);
            }else if ($request->input('refund_result_type') == Params::ORDER_REFUND_RESULT_DISAGREE){
                $refundService->disagreeRefund($refund->id,$request->input('refund_refuse_reason'));
            }
            $refund = $refund->fresh();
        }

        return view('refund/detail',['order'=>$refund->order,'refund'=>$refund,
            'refund_result_agree'=>Params::ORDER_REFUND_RESULT_AGREE,'refund_result_disagree'=>Params::ORDER_REFUND_RESULT_DISAGREE,
            'arbitrate_result_agree_full'=>Params::ORDER_ARBITRATE_RESULT_AGREE_FULL,'arbitrate_result_agree_part'=>Params::ORDER_ARBITRATE_RESULT_AGREE_PART,
            'arbitrate_result_disagree'=>Params::ORDER_ARBITRATE_RESULT_DISAGREE
        ]);
    }

    /**
     * 仲裁详情
     * @param OrderRefund $refund
     * @param Request $request
     * @param IOrderService $orderService
     * @param IRefundService $refundService
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function arbitrate(OrderRefund $refund, Request $request,IOrderService $orderService,IRefundService $refundService)
    {

        return view('refund/arbitrate',['refund'=>$refund,]);
    }

}
